package com.yanqu.road.server.manager.activity.flowerstudy;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.FlowerStudyProto;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

public class CrossFlowerStudyMgr extends TempCrossMgr {

    private static Logger logger = Logger.getLogger(CrossFlowerStudyMgr.class.getName());

    public static Map<Integer,CrossFlowerStudyActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean save() {
        for (CrossFlowerStudyActivity activity:activityMap.values()){
            activity.save();
        }
        return true;
    }


    @Override
    public boolean removeExpireData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        CrossFlowerStudyConfigMgr.reloadConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        getLogger().info("start reload LangYaBang activity  start");

        activityMap = new ConcurrentHashMap<>();
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossActivityMapByType(eActivityType.FlowerStudy.getValue());
        for (Map.Entry<Integer, ActivityInfo> entry : activityInfoMap.entrySet()) {
            if (!activityMap.containsKey(entry.getKey())) {
                getActivity(entry.getKey());
            }
        }


        getLogger().info("start reload LangYaBang activity  end");

        return true;
    }

    /**
     * 选择
     * @param activityId
     * @param userId
     * @param questionIndex
     * @param choose
     * @return
     */

    public static FlowerStudyProto.FlowerStudyExamChooseResp.Builder chooseAnswer(int activityId,long userId,int questionIndex,int choose){
        FlowerStudyProto.FlowerStudyExamChooseResp.Builder builder = FlowerStudyProto.FlowerStudyExamChooseResp.newBuilder();
        CrossFlowerStudyActivity activity =getActivity(activityId);
        if (activity == null) {
            builder.setRet(GameErrorCode.E_FLOWER_STUDY_CROSS_ACTIVITY_NO_INIT);
            return builder;
        }
        return activity.chooseExamAnswer(userId,questionIndex,choose);
    }

    /**
     * 获取当前房间详情
     * @param activityId
     * @param userId
     * @param serverId
     * @return
     */

    public static FlowerStudyProto.FlowerStudyExamRoomNewestDataResp.Builder getRoomDetail(int activityId,long userId,int serverId) {
        FlowerStudyProto.FlowerStudyExamRoomNewestDataResp.Builder builder = FlowerStudyProto.FlowerStudyExamRoomNewestDataResp.newBuilder();
        CrossFlowerStudyActivity activity =getActivity(activityId);
        if (activity == null) {
            builder.setRet(GameErrorCode.E_FLOWER_STUDY_CROSS_ACTIVITY_NO_INIT);
            return builder;
        }
        return activity.getRoomDetail(userId,serverId);

    }

    public static FlowerStudyProto.getUserCrossDataToServerResp.Builder getCrossUserDataMsg(int activityId, long userId, int serverId){
        FlowerStudyProto.getUserCrossDataToServerResp.Builder builder = FlowerStudyProto.getUserCrossDataToServerResp.newBuilder();

        CrossFlowerStudyActivity activity =getActivity(activityId);
        if (activity == null) {
            builder.setRet(GameErrorCode.E_FLOWER_STUDY_CROSS_ACTIVITY_NO_INIT);
            return builder;
        }
        return activity.getCrossUserData(userId,serverId);

    }

    /**
     * 检测是否需要截榜
     */
    public static void checkSessionChange(){
        for (CrossFlowerStudyActivity activity : activityMap.values()) {
            activity.checkSessionChange();
        }
    }

    /**
     * 检测所有游戏中的房间是否要切换题目或结算
     */
    public static void checkAllActivityGamingRoom(){
        for (CrossFlowerStudyActivity activity : activityMap.values()) {
            activity.checkGamingRoom();
        }
    }



    /**
     * 检测所有活动内所有等待房间是否要开启
     */
    public static void checkAllActivityWaitingRoom(){
        for (CrossFlowerStudyActivity activity : activityMap.values()) {
            activity.checkWaitRoom();
        }
    }

    /**
     * 取消匹配
     */
    public static FlowerStudyProto.FlowerStudyExamMatchingCancelResp.Builder cancelMatching (int activityId,long userId){
        FlowerStudyProto.FlowerStudyExamMatchingCancelResp.Builder builder = FlowerStudyProto.FlowerStudyExamMatchingCancelResp.newBuilder();
        CrossFlowerStudyActivity activity =getActivity(activityId);
        if (activity == null) {
            builder.setRet(GameErrorCode.E_FLOWER_STUDY_CROSS_ACTIVITY_NO_INIT);
            return builder;
        }
        return activity.cancelMatching(userId);
    }

    /**
     * 退出房间
     */
    public static FlowerStudyProto.FlowerStudyExamQuitRoomResp.Builder quitRoom (int activityId,long userId){
        FlowerStudyProto.FlowerStudyExamQuitRoomResp.Builder builder = FlowerStudyProto.FlowerStudyExamQuitRoomResp.newBuilder();
        CrossFlowerStudyActivity activity =getActivity(activityId);
        if (activity == null) {
            builder.setRet(GameErrorCode.E_FLOWER_STUDY_CROSS_ACTIVITY_NO_INIT);
            return builder;
        }
        return activity.quitRoom(userId);
    }

    /**
     * 更新语言表
     */
    public static void updateLanguage(int activityId,String language){
        CrossFlowerStudyActivity activity =getActivity(activityId);
        if (activity == null) {
            return;
        }
        activity.updateLanguage(language);

    }

    /**
     * 开始匹配
     * @param activityId
     * @param type
     * @param userId
     * @param serverId
     * @return
     */

    public static FlowerStudyProto.FlowerStudyExamMatchingStartResp.Builder startMatching(int activityId,int type,long userId,int serverId){
        FlowerStudyProto.FlowerStudyExamMatchingStartResp.Builder builder = FlowerStudyProto.FlowerStudyExamMatchingStartResp.newBuilder();
        CrossFlowerStudyActivity activity =getActivity(activityId);
        if (activity == null) {
            builder.setRet(GameErrorCode.E_FLOWER_STUDY_CROSS_ACTIVITY_NO_INIT);
            return builder;
        }
        return activity.startMatching(type,userId,serverId);
    }

    public static FlowerStudyProto.FlowerStudyExamSubmitAdvanceResp.Builder submitAdvance(int activityId,long userId,int questionIndex){
        FlowerStudyProto.FlowerStudyExamSubmitAdvanceResp.Builder builder = FlowerStudyProto.FlowerStudyExamSubmitAdvanceResp.newBuilder();
        CrossFlowerStudyActivity activity =getActivity(activityId);
        if (activity == null) {
            builder.setRet(GameErrorCode.E_FLOWER_STUDY_CROSS_ACTIVITY_NO_INIT);
            return builder;
        }
        return activity.submitAdvance(userId,questionIndex);
    }

    public static CrossFlowerStudyActivity getActivity(int activityId){
        if (activityMap.get(activityId) == null) {
            synchronized (activityMap){
                if(activityMap.get(activityId) == null) {
                    ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if (activityInfo == null) {
                        return null;
                    }
                    CrossFlowerStudyActivity activity = new CrossFlowerStudyActivity(activityInfo);
                    activity.init();
                    activityMap.put(activityId,activity);
                    CrossFlowerStudyConfigMgr.reloadConfig(activityInfo);
                }
            }
        }
        return activityMap.get(activityId);
    }


    @Override
    public boolean stop() {
        return true;
    }


}
